package com.example.zwgt.yrfk.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.zwgt.yrfk.R;

/**
 * 首页筛选popwindow
 */
public class SelectPopWindow extends PopupWindow implements View.OnClickListener {
    private TextView pitem1, pitem2, pitem3, pitem4, pitem5, pitem6, pitem7, pitem8;
    private Context context;
    private PopItemOnclick onclick;
    private View popView;

    public SelectPopWindow(Context context) {
        this.context = context;
        initViews();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initViews() {
        popView = View.inflate(context, R.layout.guest_popwindow, null);
        pitem1 = popView.findViewById(R.id.tv_pop1);
        pitem2 = popView.findViewById(R.id.tv_pop2);
        pitem3 = popView.findViewById(R.id.tv_pop3);
        pitem4 = popView.findViewById(R.id.tv_pop4);
        pitem5 = popView.findViewById(R.id.tv_pop5);
        pitem6 = popView.findViewById(R.id.tv_pop6);
        pitem7 = popView.findViewById(R.id.tv_pop7);
        pitem8 = popView.findViewById(R.id.tv_pop8);
        pitem1.setOnClickListener(this);
        pitem2.setOnClickListener(this);
        pitem3.setOnClickListener(this);
        pitem4.setOnClickListener(this);
        pitem5.setOnClickListener(this);
        pitem6.setOnClickListener(this);
        pitem7.setOnClickListener(this);
        pitem8.setOnClickListener(this);
        // 导入布局
        this.setContentView(popView);
        // 设置动画效果
        this.setAnimationStyle(R.style.popwindow_anim);
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        // 设置可触
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x0000000);
        this.setBackgroundDrawable(dw);
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = windowManager.getDefaultDisplay().getWidth();
        int height = windowManager.getDefaultDisplay().getHeight();
        // 单击弹出窗以外处 关闭弹出窗
        this.setWidth(width / 2);
        this.setHeight(height / 20 * 11);


        popView.setOnTouchListener((v, event) -> {
            int y = (int) event.getY();
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (y < height) {
                    dismiss();
                }
            }
            return true;
        });

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_pop1:
                if(onclick!=null){
                    onclick.OnItemClick(1);
                }
                break;
            case R.id.tv_pop2:
                if(onclick!=null){
                    onclick.OnItemClick(2);
                }
                break;
            case R.id.tv_pop3:
                if(onclick!=null){
                    onclick.OnItemClick(3);
                }
                break;
            case R.id.tv_pop4:
                if(onclick!=null){
                    onclick.OnItemClick(4);
                }
                break;
            case R.id.tv_pop5:
                if(onclick!=null){
                    onclick.OnItemClick(5);
                }
                break;
            case R.id.tv_pop6:
                if(onclick!=null){
                    onclick.OnItemClick(6);
                }
                break;
            case R.id.tv_pop7:
                if(onclick!=null){
                    onclick.OnItemClick(7);
                }
                break;
            case R.id.tv_pop8:
                if(onclick!=null){
                    onclick.OnItemClick(8);
                }
                break;
        }
    }

    public interface PopItemOnclick {
        void OnItemClick(int position);
    }

    public void setOnItemClick(PopItemOnclick onclick) {
        this.onclick = onclick;
    }
}

